# Based on the file created for Arch Linux by:
# Dan McGee <dan@archlinux.org>
# Dave Reisner <dreisner@archlinux.org>

# Maintainer: Philip Müller <philm@manjaro.org>
# Maintainer: Guinux <guillaume@manjaro.org>
# Maintainer: Rob McCathie <rob@manjaro.org>

pkgname=pacman
pkgver=5.0.1
pkgrel=4
pkgdesc="A library-based package manager with dependency support"
arch=('i686' 'x86_64')
url="http://www.archlinux.org/pacman/"
license=('GPL')
groups=('base' 'base-devel')
depends=('bash>=4.2.042-2' 'glibc>=2.17-2' 'libarchive>=3.1.2' 'curl>=7.39.0'
         'gpgme' 'pacman-mirrorlist>=20130418' 'archlinux-keyring' 'manjaro-keyring')
checkdepends=('python2' 'fakechroot')
makedepends=('asciidoc')
optdepends=('haveged: for pacman-init.service')
provides=('pacman-contrib' 'pacman-init')
conflicts=('pacman-contrib' 'pacman-init')
replaces=('pacman-contrib' 'pacman-init')
backup=(etc/pacman.conf etc/makepkg.conf)
install=pacman.install
options=('strip' 'debug')
source=(https://sources.archlinux.org/other/pacman/$pkgname-$pkgver.tar.gz
        pacman.conf.i686
        pacman.conf.x86_64
        makepkg.conf
        pacman-sync-first-option.patch
        etc-pacman.d-gnupg.mount
        pacman-init.service
        pacman-5.0.1-roundup.patch)

prepare() {
  cd $srcdir/$pkgname-$pkgver

  # v5.0.1..pacman-5.0.1..c2f97580
  patch -p1 -i $srcdir/pacman-5.0.1-roundup.patch

  # Manjaro patches
  patch -p1 -i $srcdir/pacman-sync-first-option.patch
  #patch -p1 -i $srcdir/pacman-updated-locales.patch

  # fix bad translation mistake - corrected in transifex
  #sed -i 's#pacman-db-update#pacman-db-upgrade#' src/pacman/po/nl.po

  ./configure --prefix=/usr --sysconfdir=/etc \
    --localstatedir=/var --enable-doc \
    --with-scriptlet-shell=/usr/bin/bash \
    --with-ldconfig=/usr/bin/ldconfig
  #rm $srcdir/$pkgname-$pkgver/lib/libalpm/po/*gmo
  #cd $srcdir/$pkgname-$pkgver/lib/libalpm/po/
  #make update-gmo
  #rm $srcdir/$pkgname-$pkgver/scripts/po/*gmo
  #cd $srcdir/$pkgname-$pkgver/scripts/po/
  #make update-gmo
  #rm $srcdir/$pkgname-$pkgver/src/pacman/po/*gmo
  #cd $srcdir/$pkgname-$pkgver/src/pacman/po/
  #make update-gmo
}

build() {
  cd $srcdir/$pkgname-$pkgver
  make V=1
  make -C contrib
}

check() {
  make -C "$pkgname-$pkgver" check
}

package() {
  cd $srcdir/$pkgname-$pkgver
  make DESTDIR=$pkgdir install
  make DESTDIR=$pkgdir -C contrib install

  # install Arch specific stuff
  install -dm755 $pkgdir/etc
  install -m644 $srcdir/pacman.conf.$CARCH $pkgdir/etc/pacman.conf
  
  case "$CARCH" in
    i686)    
      mycarch="i686"
      mychost="i686-pc-linux-gnu"
      myflags="-march=i686"
      ;;
    x86_64)
      mycarch="x86_64"
      mychost="x86_64-pc-linux-gnu"
      myflags="-march=x86-64"
      ;;
  esac
  install -m644 $srcdir/makepkg.conf $pkgdir/etc/
  # set things correctly in the default conf file
  sed -i $pkgdir/etc/makepkg.conf \
    -e "s|@CARCH[@]|$mycarch|g" \
    -e "s|@CHOST[@]|$mychost|g" \
    -e "s|@CARCHFLAGS[@]|$myflags|g"
    
  # put bash_completion in the right location
  install -dm755 ${pkgdir}/usr/share/bash-completion/completions
  mv ${pkgdir}/etc/bash_completion.d/pacman \
    ${pkgdir}/usr/share/bash-completion/completions
  rmdir ${pkgdir}/etc/bash_completion.d

  for f in makepkg pacman-key; do
    ln -s pacman "$pkgdir/usr/share/bash-completion/completions/$f"
  done

  install -Dm644 contrib/PKGBUILD.vim "$pkgdir/usr/share/vim/vimfiles/syntax/PKGBUILD.vim"

  # replace rankmirrors
  rm "$pkgdir/usr/bin/rankmirrors"
  ln -sfv "/usr/bin/pacman-mirrors" "$pkgdir/usr/bin/rankmirrors"

  # install pacman-init
  install -dm755 $pkgdir/usr/lib/systemd/system/
  install -m644 $srcdir/etc-pacman.d-gnupg.mount $pkgdir/usr/lib/systemd/system/etc-pacman.d-gnupg.mount
  install -m644 $srcdir/pacman-init.service $pkgdir/usr/lib/systemd/system/pacman-init.service
}

sha256sums=('8bd5f407ce8e05c4be8f1c4be4d8dcc8550ea5e494937da5220ea2c23cbb8e04'
            '811307fac47ab1b8405da798feb122dfdd61be099dad236199503e8e6969bf5f'
            '92e68dabb8724ebaa5fd0fd50e2c8a5f2bba65797295c74571f720ee7e2c513d'
            'bdd06f1e52ff4bb8e8d5d6be785e172b74809b1266cbcd33e2c42c9927493eee'
            'feb18bf548343ce6bd60b1e9252ad4b0643c21ac1a53149d93afbd6028a7a37d'
            'b6d14727ec465bb66d0a0358163b1bbfafcb4eaed55a0f57c30aabafae7eed68'
            'ca4aa967ccfb9affab23fd61ad6d16f330b2810e5f9de93deec49ea7872369ae'
            'e9d65724f1784026dfe94b8038012bb2f4988abeed993b83307537fd93a7e695')
